// 有关于投保的基类
import { ref, type Ref } from 'vue'
import * as apis from '@/apis/index'
// types.ts
export type ProductinfoParams = {
  productCode: string
  userId: string | number
}

class productinfoBaseClass {
  // 定义属性类型
  // 产品code
  public productCode: Ref<string>
  // 代理人userId
  public userId: Ref<string | number>
  // 产品信息资料数据, 通过接口拿到的
  public productInfoData: Ref<Record<string, any>>
  // 顶部tab选项卡数据
  public tabDtos: Ref<any[]>
  // 披露信息
  public piluInfo: Ref<Record<string, any>>
  // 理赔指引
  public claimGuideInfo: Ref<Record<string, any>>

  // 构造函数
  constructor({ productCode, userId }: ProductinfoParams) {
    this.productCode = ref(productCode)
    this.userId = ref(userId)

    this.productInfoData = ref({})
    this.tabDtos = ref([])
    this.piluInfo = ref({})
    this.claimGuideInfo = ref({})
  }

  // 初始化
  public async init() {
    const p1 = this.getProductDetail()
    const p2 = this.getDisclosureInfoByHdProductCode()
    const p3 = this.queryInsuranceCompanyClaimNewShowDtoByProductCode()
    await Promise.all([p1, p2, p3])
  }

  // 获取产品详情信息
  public async getProductDetail() {
    return new Promise((resolve) => {
      apis
        .querproduct({ productCode: this.productCode.value, userId: this.userId.value })
        .then((data: Record<string, any>) => {
          data.tabDtos = [
            ...data.tabDtos,
            {
              tabName: '信息披露',
              tabContent: ``
            }
          ]
          this.productInfoData.value = data
          this.tabDtos.value = data.tabDtos || []
          resolve(true)
        })
    })
  }

  // 获取产品披露信息
  public async getDisclosureInfoByHdProductCode() {
    return new Promise((resolve) => {
      apis
        .getDisclosureInfoByHdProductCode({ productCode: this.productCode.value })
        .then((data: Record<string, any>) => {
          this.piluInfo.value = data || {}
          console.log(data, 1122123)
          resolve(true)
        })
    })
  }

  // 理赔指引信息查询
  public async queryInsuranceCompanyClaimNewShowDtoByProductCode() {
    return new Promise((resolve) => {
      apis
        .queryInsuranceCompanyClaimNewShowDtoByProductCode({ productCode: this.productCode.value })
        .then((data: Record<string, any>) => {
          this.claimGuideInfo.value = data || {}

          if (!data) {
            return
          }
          let claimMaterialList: any[] = []
          if (
            this.claimGuideInfo.value.claimMaterialClassDtoList &&
            this.claimGuideInfo.value.claimMaterialClassDtoList.length
          ) {
            this.claimGuideInfo.value.claimMaterialClassDtoList.forEach((item: any) => {
              claimMaterialList = claimMaterialList.concat(item.claimMaterialList)
            })
          }
          this.claimGuideInfo.value.claimMaterialList = claimMaterialList
          this.claimGuideInfo.value.claimDocuments = JSON.parse(
            this.claimGuideInfo.value.claimDocuments
          )
          resolve(true)
        })
    })
  }
}

// 导出基类
export default productinfoBaseClass
